﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using MyWeb.Business.System;
using MyWeb.Business.Tasks;
using MyWeb.Extensions.ServiceExtensions.Swagger;
using MyWeb.Model;
using static MyWeb.Extensions.ServiceExtensions.Swagger.CustomApiVersion;

using Protos;
using Microsoft.AspNetCore.Authorization;

namespace MyWeb.Api.Controllers.V1.Quartz
{
    /// <summary>
    /// 任务调度中心
    /// </summary>
    [CustomRoute(ApiVersions.V1)]
    [ApiController]
    [AllowAnonymous]
    public class TaskController : BaseController
    {
        private readonly IQuartzService _quartz;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="quartz"></param>
        public TaskController(IQuartzService quartz)
        {
            _quartz = quartz;
        }

        /// <summary>
        /// 初始化所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Init()
        {
            await _quartz.InitJobsAsync();
            return new ApiResult();
        }

        /// <summary>
        /// 查询所有作业
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Get(int page = 1, int pageCount = 2)
        {
            //实体生成数据库表
            //BaseDB.CodeFirst.InitTables(typeof(Jobs));//具体用法看文档迁移
            //return Ok(await _quartz.GetJobs(new Google.Protobuf.WellKnownTypes.Empty(),null));
            var list = await _quartz.GetJobs();

            return Success(list);
        }
        /// <summary>
        /// 添加作业任务
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> AddJob(ResponseJob.Types.JobsModel jobs)
        {
            return await _quartz.AddJob(jobs) ? Success() : Failed();
        }
        /// <summary>
        /// 移除任务
        /// </summary>
        /// <param name="jobId">任务Id</param>
        /// <param name="taskName">任务名称</param>
        /// <param name="groupName">任务组名</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Remove(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            ResponseJob.Types.JobsModel jobs = new ResponseJob.Types.JobsModel() 
            { 
                Id = jobId, 
                TaskName = taskName, 
                GroupName = groupName 
            };
            return await _quartz.Remove(jobs) ? Success() : Failed();
        }
        /// <summary>
        /// 修改任务信息
        /// </summary>
        /// <param name="jobs"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult> Update(ResponseJob.Types.JobsModel jobs)
        {
            return await _quartz.Update(jobs) ? Success() : Failed();

        }
        /// <summary>
        /// 暂停任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Pause(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            ResponseJob.Types.JobsModel jobs = new ResponseJob.Types.JobsModel() 
            { 
                Id = jobId, 
                TaskName = taskName, 
                GroupName = groupName 
            };
            return await _quartz.Pause(jobs) ? Success() : Failed();
        }
        /// <summary>
        /// 开始执行任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Start(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            TaskRequest  jobs = new TaskRequest()
            {
                JobId = jobId,
                TaskName = taskName,
                GroupName = groupName
            };
            return await _quartz.Start(jobs) ? Success() : Failed();
        }
        /// <summary>
        /// 运行一次任务
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> Run(long jobId = 1671088025716789248)
        {
            return await _quartz.Run(new ResponseJob.Types.JobsModel() { Id = jobId }) ? Success() : Failed();
        }
        /// <summary>
        /// 查看任务状态
        /// </summary>
        /// <param name="jobId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> GetState(long jobId = 1671088025716789248)
        {
            return Success(await _quartz.GetState(new ResponseJob.Types.JobsModel() { Id = jobId }));
        }
        /// <summary>
        /// 查看任务是否存在
        /// </summary>
        /// <param name="jobId"></param>
        /// <param name="taskName"></param>
        /// <param name="groupName"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult> IsQuartzJob(long jobId = 1671088025716789248, string? taskName = "", string? groupName = "")
        {
            TaskRequest jobs = new TaskRequest()
            {
                JobId = jobId,
                TaskName = taskName,
                GroupName = groupName
            };
            return await _quartz.IsQuartzJob(jobs) ? Success() : Failed();
        }
        /// <summary>
        /// 校验Cron表达式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        [HttpGet]
        public ApiResult IsValidExpression(string str)
        {
            return _quartz.IsValidExpression(str) ? Success() : Failed();
        }
        //[HttpGet]
        //public async Task<MessageModel<object>> GetTaskLog(long jobId, int page, int pageSize) => Success(await _quartz.GetLog(jobId, page, pageSize));

        [HttpPost]
        public ApiResult PostTest([FromBody] ResponseJob.Types.JobsModel jobs)
        {
            return Success(jobs);
        }
    }
}
